package net.magicalcloud.magicalcloud.model.modelImpl;

import net.magicalcloud.magicalcloud.entity.RestJson;
import net.magicalcloud.magicalcloud.entity.User;
import net.magicalcloud.magicalcloud.execption.CloudExecption;
import net.magicalcloud.magicalcloud.listener.CloudListener;
import net.magicalcloud.magicalcloud.model.AppURL;
import net.magicalcloud.magicalcloud.model.IUserModel;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


import static net.magicalcloud.magicalcloud.util.RetrofitUtil.getAppUrl;

/** 用户model接口实现
 * Created by Pei on 2018/6/6.
 */

public class UserModel implements IUserModel{

    @Override
    public void findUserByEmail(String email, String password, final CloudListener<User> listener) {

        //获取AppUrl
        AppURL url= getAppUrl();
        //定义call
        Call<RestJson<User>> call = url.findUserByEmail(email,password);

        call.enqueue(new Callback<RestJson<User>>() {
            @Override
            public void onResponse(Call<RestJson<User>> call, Response<RestJson<User>> response) {
                if(response.body().getCode()==0){
                    //无异常，返回数据
                    listener.done(response.body().getData(),null);
                }else{
                    //构建异常
                    listener.done(null,new CloudExecption(response.body().getCode(),response.body().getMessage()));
                }
            }
            @Override
            public void onFailure(Call<RestJson<User>> call, Throwable t) {
                //构建异常
                listener.done(null,new CloudExecption(t));
            }
        });


    }
}
